package com.example.musicplayer.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.example.musicplayer.R;
import com.example.musicplayer.commonUtils.StringUtil;
import com.example.musicplayer.commonUtils.ToastUtil;
import com.example.musicplayer.settings.SettingsItem;
import com.example.musicplayer.user.UserManage;

public class PasswordConfirmActivity extends BaseSettingsActivity {
    public static final String CONFIRM_ACTION = "confirmSuccessAction";
    private EditText mInputPassword;
    private View mResult;
    private View mClear;
    private View mConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setEnableMusicTabView(false);
        loadEditView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mInputPassword.requestFocus();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == PasswordModifyActivity.RESULT_CODE) {
            mInputPassword.setText("");
            if (data != null && data.getBooleanExtra(PasswordModifyActivity.MODIFY_SUCCESS, false)) {
                ToastUtil.makeToast(getString(R.string.password_modify_success));
                finishActivity();
            }
        } else if (resultCode == SecurityQuestionActivity.RESULT_CODE) {
            mInputPassword.setText("");
            if (data != null && data.getBooleanExtra(SecurityQuestionActivity.MODIFY_SUCCESS, false)) {
                finishActivity();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected int setActivityTitle() {
        return R.string.title_password_confirm_activity;
    }

    private void loadEditView() {
        LayoutInflater inflater = LayoutInflater.from(this);
        @SuppressLint("InflateParams") View editView = inflater.inflate(R.layout.password_confirm_view, null);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT
                , RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        ((RelativeLayout) findViewById(R.id.relative_layout_base_settings)).addView(editView, lp);
        //edit text
        mInputPassword = editView.findViewById(R.id.edittext_input_password);
        mInputPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mResult.setVisibility(View.GONE);
                if (s.toString().length() == 0) {
                    mClear.setVisibility(View.GONE);
                    mConfirm.setEnabled(false);
                } else {
                    mClear.setVisibility(View.VISIBLE);
                    mConfirm.setEnabled(true);
                }
            }
        });
        mResult = editView.findViewById(R.id.password_result);
        //clear input text
        mClear = editView.findViewById(R.id.clear_password);
        mClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mInputPassword.setText("");
            }
        });
        //confirm button
        mConfirm = editView.findViewById(R.id.button_confirm);
        mConfirm.setEnabled(false);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doConfirmAction();
            }
        });
    }

    private void doConfirmAction() {
        String password = mInputPassword.getText().toString();
        if (UserManage.confirmPasswordForUser(StringUtil.ByteToHexString(password.getBytes()))) {
            int action = getIntent().getIntExtra(CONFIRM_ACTION, -1);
            Intent intent = null;
            if (action == SettingsItem.MODIFY_PASSWORD) {
                intent = new Intent(this, PasswordModifyActivity.class);
            }
            if (action == SettingsItem.SET_PASSWORD_PROTECT) {
                intent = new Intent(this, SecurityQuestionActivity.class);
                intent.putExtra(SecurityQuestionActivity.ACTION_MODE, SecurityQuestionActivity.MODE_MODIFY);
            }
            if (intent != null) {
                startActivityForResult(intent, 1);
            }
        } else {
            mResult.setVisibility(View.VISIBLE);
        }
    }
}